package org.jeecg.modules.htxincailiao.report.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.library.entity.HTLibraryCategory;
import org.jeecg.modules.htxincailiao.report.entity.HTReportCategory;
import org.jeecg.modules.htxincailiao.report.service.IHTReportCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: ht_report_category
 * @Author: jeecg-boot
 * @Date: 2024-6-20 10:44:00
 * @Version: V1.0
 */
@Api(tags = "ht_report_category")
@RestController
@RequestMapping("/report/hTReportCategory")
@Slf4j
public class HTReportCategoryController extends JeecgController<HTReportCategory, IHTReportCategoryService> {

    @Autowired
    private IHTReportCategoryService ihtReportCategoryService;

    @AutoLog(value = "ht_report_category-分页列表查询")
    @ApiOperation(value="ht_report_category-分页列表查询", notes="ht_report_category-分页列表查询")
    @RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
    public Result<?> queryPageList(HTReportCategory hTReportCategory,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        if (req.getMethod().equals("POST")){
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTReportCategory = JSONObject.parseObject(line, HTReportCategory.class);
                pageNo = oConvertUtils.isNotEmpty(hTReportCategory.getPageNo()) ? hTReportCategory.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTReportCategory.getPageSize()) ? hTReportCategory.getPageSize() : pageSize;
            }
        }
        QueryWrapper<HTReportCategory> queryWrapper = QueryGenerator.initQueryWrapper(hTReportCategory, req.getParameterMap());
        Page<HTReportCategory> page = new Page<>(pageNo, pageSize);
        IPage<HTReportCategory> pageList = ihtReportCategoryService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "ht_report_category-添加")
    @ApiOperation(value="ht_report_category-添加", notes="ht_report_category-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTReportCategory hTReportCategory) {
        ihtReportCategoryService.save(hTReportCategory);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "ht_report_category-编辑")
    @ApiOperation(value="ht_report_category-编辑", notes="ht_report_category-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTReportCategory hTReportCategory) {
        ihtReportCategoryService.updateById(hTReportCategory);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "ht_report_category-通过id删除")
    @ApiOperation(value="ht_report_category-通过id删除", notes="ht_report_category-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        ihtReportCategoryService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "ht_report_category-批量删除")
    @ApiOperation(value="ht_report_category-批量删除", notes="ht_report_category-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.ihtReportCategoryService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog(value = "ht_report_category-通过id查询")
    @ApiOperation(value="ht_report_category-通过id查询", notes="ht_report_category-通过id查询")
    @RequestMapping(value = "/queryById", method = {RequestMethod.GET,RequestMethod.POST})
    public Result<?> queryById(HttpServletRequest req, @RequestParam(name="id",required=false) String id) {
        if (req.getMethod().equals("POST")){
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                HTReportCategory hTReportCategory  = JSONObject.parseObject(line, HTReportCategory.class);
                id = hTReportCategory.getId();
            }
        }
        HTReportCategory hTReportCategory = ihtReportCategoryService.getById(id);
        if(hTReportCategory==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTReportCategory);
    }

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTReportCategory hTReportCategory) {
        return super.exportXls(request, hTReportCategory, HTReportCategory.class, "ht_report_category");
    }

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTReportCategory.class);
    }

}
